//
//  HotVideoViewController.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/12.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "HotVideoViewController.h"
#import "ZLSwipeableView.h"
#import "CustomerNetworking.h"
#import "Channel.h"
#import "AutoadaptationFrame.h"
#import "CustomerAVPlayer.h"
#import "SCCatWaitingHUD.h"
#import "TouchView.h"
#import "VideoDetailViewController.h"
#import "SCCatWaitingHUD.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>

@interface HotVideoViewController ()<ZLSwipeableViewDataSource, ZLSwipeableViewDelegate>

@property(nonatomic, assign)int count;

@property (nonatomic, retain) ZLSwipeableView *swipeableView;

@property(nonatomic, assign)BOOL isPush;

@property(nonatomic, retain)NSMutableArray *viewArray;

@property(nonatomic, retain)NSMutableArray *dataArray;

@property(nonatomic, retain)NSMutableArray *channelArray;

@property(nonatomic, retain)CustomerAVPlayer *avPlayer;

@property(nonatomic, assign)NSInteger lastView;

@end

@implementation HotVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"热榜";
    self.isPush = NO;
    self.count=0;
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    
    self.viewArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    self.channelArray = [NSMutableArray array];
    
    self.avPlayer = [CustomerAVPlayer shareCustomerAVPlayer];
    
    [self loadData];
    
    [[SCCatWaitingHUD sharedInstance] animateWithInteractionEnabled:YES];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, kWidth, 30);
    button.center = self.view.center;
    [button setTitle:@"点击重新加载" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(loadData) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)loadData
{
    [[SCCatWaitingHUD sharedInstance] animateWithInteractionEnabled:YES];
    [CustomerNetworking customerConnectWithURL:@"http://api.miaopai.com/m/ascent_channel.json?os=ios&page=1&per=20&token=F~W2VqcmbHqGYzTILdMLiY76TB0mRA84&unique_id=7d1af0345afdbb8fae421c969f8101822368410872&version=6.1.0" andparameters:nil andBlock:^(id response) {
        [[SCCatWaitingHUD sharedInstance] stop];
        //NSLog(@"%@", response);
        
        for (NSDictionary *dic in [response objectForKey:@"result"])
        {
            if ([[dic objectForKey:@"type"] isEqualToString:@"channel"])
            {
                Channel *channel = [Channel channelWithDic:[dic objectForKey:@"channel"]];
                [self.dataArray addObject:channel];
            }
        }
        
        self.swipeableView = [[ZLSwipeableView alloc] initWithFrame:self.view.frame];
        self.swipeableView.dataSource = self;
        self.swipeableView.delegate = self;
        [self.view addSubview:self.swipeableView];
    }];
}

#pragma mark - ZLSwipeableViewDataSource
- (UIView *)nextViewForSwipeableView:(ZLSwipeableView *)swipeableView {
    
    Channel *channel = nil;
    
    TouchView *view = [[TouchView alloc] initWithFrame:CGRectMake(0, 0, kWidth - 80, (kWidth - 80) * 1.3)];
    view.layer.shadowOffset = CGSizeMake(1, 3);
    view.layer.shadowColor = [[UIColor blackColor] CGColor];
    view.layer.shadowOpacity = 0.8;
    view.backgroundColor = [UIColor whiteColor];
    if (self.count < self.dataArray.count) {
        channel = [self.dataArray objectAtIndex:self.count];
        view.channel = channel;
        [self.channelArray addObject:channel];
        UIImageView *pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.width)];
        //pic.layer.cornerRadius = 20;
        pic.layer.masksToBounds = YES;
        //pic.backgroundColor = [UIColor yellowColor];
        NSString *picURL = [NSString stringWithFormat:@"%@%@", channel.pic.base, channel.pic.m];
        [pic sd_setImageWithURL:[NSURL URLWithString:picURL] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        [view addSubview:pic];
        [pic release];
        
        UILabel *hotLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, view.frame.size.width , view.frame.size.width - 20, 25)];
        hotLabel.textColor = [UIColor grayColor];
        NSString *hotStr = [NSString stringWithFormat:@"  %@热度", channel.stat.vcntNice];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:hotStr];
        NSRange range = [hotStr rangeOfString:channel.stat.vcntNice];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        [hotLabel setAttributedText:attrStr];
        hotLabel.font = [UIFont systemFontOfSize:12];
        [view addSubview:hotLabel];
        [attrStr release];
        [hotLabel release];

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, hotLabel.frame.origin.y + hotLabel.frame.size.height - 10, view.frame.size.width - 20, view.frame.size.height - pic.frame.size.height - hotLabel.frame.size.height )];
        if ([AutoadaptationFrame heightOfLabel:channel.ext.t andTextFont:[UIFont systemFontOfSize:15] andLabelWidth:view.frame.size.width - 20] > titleLabel.frame.size.height) {
            titleLabel.text = [NSString stringWithFormat:@"%@", channel.ext.t];
        }else
        {
            titleLabel.text = [NSString stringWithFormat:@"%@\n\n", channel.ext.t];
        }
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = [UIColor colorWithWhite:0.229 alpha:1.000];
        titleLabel.numberOfLines = 0;
        [view addSubview:titleLabel];
        [titleLabel release];
        
        
        [channel release];
    }else
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, view.frame.size.width, 60)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"没有更多了哦， \n可以去别处逛逛～";
        label.numberOfLines = 0;
        [view addSubview:label];
        [label release];
    }
    
    [view addMyTarget:self action:@selector(toDetail:)];
    [self.viewArray addObject:view];

    if (self.count <= self.dataArray.count) {
        self.swipeableView.direction = ZLSwipeableViewDirectionAll;
    }else
    {
        self.swipeableView.direction = ZLSwipeableViewDirectionNone;
    }
    
    self.count++;
    
    if(self.count == self.dataArray.count)
    {
        self.count = 0;
    }
    NSLog(@"%ld", (long)self.count);
    
    return [view autorelease];
}

- (void)viewWillAppear:(BOOL)animated
{
    //NSLog(@"======%d", self.count);
    if (!self.isPush) {
        UIView * view = [self.tabBarController.view.subviews lastObject];
        [UIView animateWithDuration:0.5 animations:^{
            view.frame = CGRectMake(0, kHeight - 48, kWidth, 48);
        }];
    }
}

- (void)toDetail:(TouchView *)view
{
    VideoDetailViewController *videoView = [[VideoDetailViewController alloc] init];
    videoView.scid = view.channel.scid;
    self.isPush = YES;
    [self.navigationController pushViewController:videoView animated:YES];
    [videoView release];
}

- (void)viewWillDisappear:(BOOL)animated
{
    if (self.isPush) {
        self.isPush = NO;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
